#include "fyp/GuiManager.h"

//------------------------------------------------------------------------------
// additional header includes
#include "DXUT.h"
#include "DXUTgui.h"

//------------------------------------------------------------------------------
namespace ming
{
namespace fyp
{
	//--------------------------------------------------------------------------
	TGuiManager::TGuiManager(IDirect3DDevice9 *device)
	{
		mGuiVisible = true;

		mDlgResMgr = new CDXUTDialogResourceManager();

		if(mDlgResMgr)
		{
			mDlgResMgr->OnD3D9CreateDevice(device);
		}
	}
	
	//--------------------------------------------------------------------------
	TGuiManager::~TGuiManager()
	{
		Clear();

		if(mDlgResMgr)
		{
			mDlgResMgr->OnD3D9LostDevice();
			mDlgResMgr->OnD3D9DestroyDevice();
			delete mDlgResMgr;
		}
	}

	//--------------------------------------------------------------------------
	void TGuiManager::Add(const TStr &name)
	{
		DialogMap_t::iterator iter = mDlgs.find(name);

		if(iter != mDlgs.end())
			// already exist
			return;

		mDlgs[name] = new CDXUTDialog();
		mDlgs[name]->Init(mDlgResMgr);
	}

	//--------------------------------------------------------------------------
	CDXUTDialog* TGuiManager::Get(const TStr &name)
	{
		DialogMap_t::iterator iter = mDlgs.find(name);

		if(iter == mDlgs.end())
			// not exist
			return NULL;

		return iter->second;
	}

	//--------------------------------------------------------------------------
	void TGuiManager::Remove(const TStr &name)
	{
		DialogMap_t::iterator iter = mDlgs.find(name);

		if(iter == mDlgs.end())
			return;

		delete iter->second;
		mDlgs.erase(iter);
	}

	//--------------------------------------------------------------------------
	void TGuiManager::Clear()
	{
		for(DialogMap_t::iterator iter = mDlgs.begin(); iter != mDlgs.end(); ++iter)
		{
			delete iter->second;
		}

		mDlgs.clear();
	}

	//--------------------------------------------------------------------------
	void TGuiManager::HandleWindowEvent(
		HWND hWnd,
		UINT uMsg,
		WPARAM wParam,
		LPARAM lParam,
		bool* pbNoFurtherProcessing)
	{
		if(!mGuiVisible)
			return;

		for(DialogMap_t::iterator iter = mDlgs.begin(); iter != mDlgs.end(); ++iter)
		{
			*pbNoFurtherProcessing = iter->second->MsgProc(hWnd, uMsg, wParam, lParam);

			if( *pbNoFurtherProcessing )
				break;
		}
	}	

	//--------------------------------------------------------------------------
	void TGuiManager::ReloadResouces()
	{
		if(mDlgResMgr)
			mDlgResMgr->OnD3D9ResetDevice();
	}

	//--------------------------------------------------------------------------
	void TGuiManager::Render(float dt)
	{
		if(!mGuiVisible)
			return;

		for(DialogMap_t::iterator iter = mDlgs.begin(); iter != mDlgs.end(); ++iter)
		{
			iter->second->OnRender(dt);
		}
	}

	//--------------------------------------------------------------------------
	bool TGuiManager::IsVisible()
	{
		return mGuiVisible;
	}

	//--------------------------------------------------------------------------
	void TGuiManager::SetVisible(bool value)
	{
		mGuiVisible = value;
	}

	//--------------------------------------------------------------------------
}
}
